package com.lv.controller;

import com.lv.data.Result;
import com.lv.service.ItemService;
import com.lv.service.TypeService;
import com.lv.vo.ItemVo;
import com.lv.vo.SellerVo;
import com.lv.vo.TemVo;
import com.lv.vo.TypeVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Resource
    private ItemService is;

    @Resource
    private TypeService ts;
    //查询
    @PostMapping("select")
    public Result getInfo(@RequestParam Long parentId){
        List<ItemVo> vo = is.getInfo(parentId);
        return Result.OK().setData(vo);
    }

    //查询单条
    @PostMapping("sel")
    public Result select(@RequestParam Long id){
        if(id == 0){
            ItemVo vo = new ItemVo();
            return Result.OK().setData(vo);
        }
        ItemVo vo = is.select(id);
        return Result.OK().setData(vo);
    }

    //增加
    @PostMapping()
    public Result addIten(@RequestBody ItemVo vo){
        is.addItem(vo);
        return Result.OK();
    }

    //根据Id查询父级的Id
    @PostMapping("to")
    public Result to(@RequestParam Long id){
        ItemVo vo = is.to(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping
    public Result update(@RequestBody ItemVo vo){
        is.update(vo);
        return Result.OK();
    }

    //删除
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids ){
        is.delete(ids);
        return Result.OK();
    }

    //增加商品的品牌
    @PostMapping("getitem")
    public Result getitem(@RequestParam Long id){
        if (id == 0){
            return Result.OK().setData(new TypeVo());
        }

        ItemVo vo = is.select(id);
        Long typeId = vo.getTypeId();
        TypeVo tvo = ts.getitem(typeId);
        return Result.OK().setData(tvo);
    }

    //模板
    @PostMapping("getType")
    public Result getType(@RequestParam Long itemCatId){
        TypeVo vo = is.getType(itemCatId);
        return Result.OK().setData(vo);
    }

    //扣减库存
    @PostMapping("subStockCount")
    public Result subStockCount(@RequestBody List<TemVo> itemVoList){
        is.subStockCount(itemVoList);
        return Result.OK();
    }
}
